<?php
namespace app\upgrade\common;
use app\upgrade\common\Common;
use think\Db;
use think\response\Json;

/**
 * 公共部分
 */
class Common
{
/**
* 压缩文件夹及文件
* @param type $source        需要压缩的文件夹/文件路径
* @param type $destination    压缩后的保存地址
* @param type $folder        文件夹前缀，保存时需要去掉的父级文件夹
* @return boolean
*/
function backup_zip($source, $destination,$folder='')
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        return "false";
    }
    $zip = new \ZipArchive();
    if (!$zip->open($destination, \ZIPARCHIVE::CREATE)) {
        return "false";
    }
    $source = str_replace('\\', '/', $source);
    $folder = str_replace('\\', '/', $folder);
    if (is_dir($source) === true) {
        $files = new \RecursiveDirectoryIterator($source,\RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file) {
            $file = str_replace('\\', '/', $file);
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                continue;
            if (is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($folder . '/', '', $file . '/'));
            } else if (is_file($file) === true) {
                $zip->addFromString(str_replace($folder . '/', '', $file), file_get_contents($file));
            }
        }
    } else if (is_file($source) === true) {
        $zip->addFromString(basename($source), file_get_contents($source));
    }
    $zip->close();
    return "true";
}




public function getRoot()
{
 $d = $_SERVER['DOCUMENT_ROOT']; 
 echo $d; //打印网站根目录;
}
}